package ${entity.actionPackageName};

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apabi.common.pojo.JqGridResult;
import com.apabi.common.utils.LeeJSONResult;
import com.apabi.pojo.${entity.className};
import com.apabi.service.${entity.className}Service;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Controller
@RequestMapping("/${entity.classInstanceName}")
public class ${entity.className}Controller extends BaseController {

	final static Logger log = LoggerFactory.getLogger(${entity.className}Controller.class);

	@Autowired
	private ${entity.className}Service ${entity.classInstanceName}Service;

	/**
	 * 显示添加${entity.tableName}页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/create${entity.className}Page")
	public ModelAndView create${entity.className}Page(HttpServletRequest request) {
		log.debug("显示添加${entity.tableName}页面");
		ModelAndView mv = new ModelAndView("${entity.classInstanceName}/create${entity.className}Page");
		return mv;
	}
	
	
	/**
	 * ${entity.tableName}页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/${entity.classInstanceName}List")
	public String show${entity.classInstanceName}ListPage(HttpServletRequest request) {
		return "${entity.classInstanceName}/${entity.classInstanceName}List";
	}


	/**
	 * 查询${entity.tableName}列表内容
	 * 
	 * @param article
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/get${entity.className}List")
	@ResponseBody
	public JqGridResult getUserInfoList(${entity.className}Controller article, Integer page, Integer pageSize) {
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		JqGridResult result = ${entity.classInstanceName}Service.query${entity.className}List(null, page, pageSize);

		return result;
	}

	/**
	 * 修改或增加${entity.tableName}
	 * 
	 * @param ${entity.classInstanceName}
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public LeeJSONResult saveOrUpdate(${entity.className} ${entity.classInstanceName}) {

		String ${entity.classInstanceName}Id = ${entity.classInstanceName}.getId();
		if (StringUtils.isEmpty(${entity.classInstanceName}Id)) {
			${entity.classInstanceName}Service.save${entity.className}(${entity.classInstanceName});
		} else {
			${entity.classInstanceName}Service.update${entity.className}ById(${entity.classInstanceName});
		}
		return LeeJSONResult.ok();
	}

	/**
	 * 查询某一个${entity.tableName}
	 * 
	 * @param ${entity.classInstanceName}Id
	 * @param request
	 * @return
	 */
	@RequestMapping("/${entity.classInstanceName}Detail")
	public ModelAndView ${entity.classInstanceName}Detail(String ${entity.classInstanceName}Id, HttpServletRequest request) {

		${entity.className} ${entity.classInstanceName} = ${entity.classInstanceName}Service.query${entity.className}ById(${entity.classInstanceName}Id);

		ModelAndView mv = new ModelAndView("${entity.classInstanceName}/${entity.classInstanceName}Detail");
		mv.addObject("${entity.classInstanceName}", ${entity.classInstanceName});
		return mv;
	}

	/**
	 * 删除${entity.tableName}
	 * 
	 * @param ${entity.classInstanceName}Id
	 * @return
	 */
	@RequestMapping("/delete${entity.className}")
	@ResponseBody
	public LeeJSONResult delete${entity.className}(String ${entity.classInstanceName}Id) {

		if (StringUtils.isEmpty(${entity.classInstanceName}Id)) {
			return LeeJSONResult.errorMsg("${entity.tableName}id为空或者不存在...");
		}
		${entity.classInstanceName}Service.delete${entity.className}ById(${entity.classInstanceName}Id);
		return LeeJSONResult.ok();
	}

	/**
	 * 跳转到修改${entity.tableName}页面
	 * 
	 * @param ${entity.classInstanceName}Id
	 * @param request
	 * @return
	 */
	@RequestMapping("/modify${entity.className}")
	public ModelAndView modify${entity.className}(String ${entity.classInstanceName}Id, HttpServletRequest request) {

		${entity.className} ${entity.classInstanceName} = ${entity.classInstanceName}Service.query${entity.className}ById(${entity.classInstanceName}Id);

		ModelAndView mv = new ModelAndView("${entity.classInstanceName}/modify${entity.className}");
		mv.addObject("${entity.classInstanceName}", ${entity.classInstanceName});

		return mv;
	}
}
